﻿using System.Linq;
using Engine;
using Game;

namespace IndustrialAge
{
	public class AllRecipesScreen : RecipaediaRecipesScreen
	{
		public AllRecipeWidget m_AllRecipeWidget;
		public CastRecipeWidget m_CastRecipeWidget;
		public BFRecipeWidget m_BFRecipeWidget;
		public CFRecipeWidget m_CFRecipeWidget;
		public SRecipeWidget m_MRecipeWidget;
		public AllRecipesScreen() : base()
		{
			//XElement node = ContentManager.Get<XElement>("Screens/RecipaediaRecipesScreen2");
			//WidgetsManager.LoadWidgetContents(this, this, node);
			//m_craftingRecipeWidget = Children.Find<NewCraftingRecipeWidget>("CraftingRecipe");
			//m_smeltingRecipeWidget = Children.Find<SmeltingRecipeWidget>("SmeltingRecipe");
			m_AllRecipeWidget = Children.Find<AllRecipeWidget>("AllRecipe");
			m_CastRecipeWidget = Children.Find<CastRecipeWidget>("CastRecipe");
			m_CFRecipeWidget = Children.Find<CFRecipeWidget>("CFRecipe");
			m_BFRecipeWidget = Children.Find<BFRecipeWidget>("BFRecipe");
			m_MRecipeWidget = Children.Find<SRecipeWidget>("SRecipe");
			//m_prevRecipeButton = Children.Find<ButtonWidget>("PreviousRecipe");
			//m_nextRecipeButton = Children.Find<ButtonWidget>("NextRecipe");
		}

		public override void Enter(object[] parameters)
		{
            int value = (int)parameters[0];
            m_craftingRecipes.Clear();
            m_craftingRecipes.AddRange(CraftingRecipesManager.Recipes.Where((CraftingRecipe r) => r.ResultValue == value && r.ResultValue != 0));
            m_recipeIndex = 0;
			//fixed me
			//c__DisplayClass7_0 c__DisplayClass7_ = new c__DisplayClass7_0();
			//c__DisplayClass7_.value = (int)parameters[0];
			//m_craftingRecipes.Clear();
			//m_craftingRecipes.AddRange(CraftingRecipesManager.Recipes.Where(c__DisplayClass7_.Enter_b__0));
			//m_recipeIndex = 0;
		}
		public override void Update()
		{
			if (m_recipeIndex < m_craftingRecipes.Count)
			{
				CraftingRecipe craftingRecipe = m_craftingRecipes[m_recipeIndex];
				if (craftingRecipe.RequiredHeatLevel == 0f)
				{
					m_craftingRecipeWidget.Recipe = craftingRecipe;
					m_craftingRecipeWidget.NameSuffix = $" (recipe #{m_recipeIndex + 1})";
					m_craftingRecipeWidget.IsVisible = true;
					m_smeltingRecipeWidget.IsVisible = false;
					m_AllRecipeWidget.IsVisible = false;
					m_CastRecipeWidget.IsVisible = false;
					m_CFRecipeWidget.IsVisible = false;
					m_BFRecipeWidget.IsVisible = false;
					m_MRecipeWidget.IsVisible = false;
				}
				else if (craftingRecipe.RequiredHeatLevel == -1f)
				{
					m_AllRecipeWidget.Recipe = craftingRecipe;
					m_AllRecipeWidget.NameSuffix = $" (recipe #{m_recipeIndex + 1})";
					m_craftingRecipeWidget.IsVisible = false;
					m_smeltingRecipeWidget.IsVisible = false;
					m_AllRecipeWidget.IsVisible = true;
					m_CastRecipeWidget.IsVisible = false;
					m_CFRecipeWidget.IsVisible = false;
					m_BFRecipeWidget.IsVisible = false;
					m_MRecipeWidget.IsVisible = false;
				}
				else if (craftingRecipe.RequiredHeatLevel == -2f)
				{
					m_CastRecipeWidget.Recipe = craftingRecipe;
					m_AllRecipeWidget.NameSuffix = $" (recipe #{m_recipeIndex + 1})";
					m_craftingRecipeWidget.IsVisible = false;
					m_smeltingRecipeWidget.IsVisible = false;
					m_CastRecipeWidget.IsVisible = true;
					m_AllRecipeWidget.IsVisible = false;
					m_CFRecipeWidget.IsVisible = false;
					m_BFRecipeWidget.IsVisible = false;
					m_MRecipeWidget.IsVisible = false;
					//m_CastRecipeWidget.IsVisible = false;
				}
				else if (craftingRecipe.RequiredHeatLevel == -3f)
				{
					m_BFRecipeWidget.Recipe = craftingRecipe;
					m_AllRecipeWidget.NameSuffix = $" (recipe #{m_recipeIndex + 1})";
					m_craftingRecipeWidget.IsVisible = false;
					m_smeltingRecipeWidget.IsVisible = false;
					m_CastRecipeWidget.IsVisible = false;
					m_AllRecipeWidget.IsVisible = false;
					m_CFRecipeWidget.IsVisible = false;
					m_BFRecipeWidget.IsVisible = true;
					m_MRecipeWidget.IsVisible = false;
					//m_CastRecipeWidget.IsVisible = false;
				}
				else if (craftingRecipe.RequiredHeatLevel == -4f || craftingRecipe.RequiredHeatLevel == 3000f)
				{
					m_CFRecipeWidget.Recipe = craftingRecipe;
					m_AllRecipeWidget.NameSuffix = $" (recipe #{m_recipeIndex + 1})";
					m_craftingRecipeWidget.IsVisible = false;
					m_smeltingRecipeWidget.IsVisible = false;
					m_CastRecipeWidget.IsVisible = false;
					m_AllRecipeWidget.IsVisible = false;
					m_CFRecipeWidget.IsVisible = true;
					m_BFRecipeWidget.IsVisible = false;
					m_MRecipeWidget.IsVisible = false;
					//m_CastRecipeWidget.IsVisible = false;
				}
				else if (craftingRecipe.RequiredHeatLevel == -5f)
				{
					m_MRecipeWidget.Recipe = craftingRecipe;
					m_AllRecipeWidget.NameSuffix = $" (recipe #{m_recipeIndex + 1})";
					m_craftingRecipeWidget.IsVisible = false;
					m_smeltingRecipeWidget.IsVisible = false;
					m_CastRecipeWidget.IsVisible = false;
					m_AllRecipeWidget.IsVisible = false;
					m_CFRecipeWidget.IsVisible = false;
					m_BFRecipeWidget.IsVisible = false;
					m_MRecipeWidget.IsVisible = true;
					//m_CastRecipeWidget.IsVisible = false;
				}
				else
				{
					m_smeltingRecipeWidget.Recipe = craftingRecipe;
					m_smeltingRecipeWidget.NameSuffix = $" (recipe #{m_recipeIndex + 1})";
					m_smeltingRecipeWidget.IsVisible = true;
					m_craftingRecipeWidget.IsVisible = false;
					m_AllRecipeWidget.IsVisible = false;
					m_CastRecipeWidget.IsVisible = false;
					m_CFRecipeWidget.IsVisible = false;
					m_BFRecipeWidget.IsVisible = false;
					m_MRecipeWidget.IsVisible = false;
				}
			}
			m_prevRecipeButton.IsEnabled = (m_recipeIndex > 0);
			m_nextRecipeButton.IsEnabled = (m_recipeIndex < m_craftingRecipes.Count - 1);
			if (m_prevRecipeButton.IsClicked)
			{
				m_recipeIndex = MathUtils.Max(m_recipeIndex - 1, 0);
			}
			if (m_nextRecipeButton.IsClicked)
			{
				m_recipeIndex = MathUtils.Min(m_recipeIndex + 1, m_craftingRecipes.Count - 1);
			}
			if (base.Input.Back || base.Input.Cancel || Children.Find<ButtonWidget>("TopBar.Back").IsClicked)
			{
				ScreensManager.SwitchScreen(ScreensManager.PreviousScreen);
			}
		}

	}

	public class RecipaediaScreen2 : RecipaediaScreen
	{
		public override void Enter(object[] parameters)
		{
			if (!(ScreensManager.PreviousScreen is AllRecipesScreen) && ScreensManager.PreviousScreen != ScreensManager.FindScreen<Screen>("RecipaediaDescription"))
			{
				m_previousScreen = ScreensManager.PreviousScreen;
			}
		}
	}
}
